/**
 * Created by Darkness on 14-1-6.
 */
(function($) {
    var arkflow = $.arkflow;

    // 创建路径对象
    arkflow.PathText = Base.extend({
        constructor : function(path) {
            this.path = path;
            this.paper = path.paper;
            this.comp = this.paper.text(0, 0, path.pathConfig.text.text).attr(path.pathConfig.text).attr({
                text : path.pathConfig.text.text.replace("{from}", path.fromNode.text()).replace("{to}", path.toNode.text())
            });
            this.node = this.comp.node;
            var me = this;
            this.comp.drag(function(r, o) {
                if (!arkflow.config.editable) {
                    return;
                }
                me.comp.attr({
                    x : me.path.x + r,
                    y : me.path.y + o
                });
            }, function() {
                me.x = me.comp.attr("x");
                me.y = me.comp.attr("y");
            }, function() {
                var o = me.path.linePoints.midDot().pos();
                me.textPos = {
                    x : me.comp.attr("x") - o.x,
                    y : me.comp.attr("y") - o.y
                };
            });
            this.bindTextChange();
        },
        bindTextChange : function() {
            var me = this;
            $(this.paper).bind("textchange", function(event, newText, path) {
                if (path.getId() == me.path.id) {
                    me.setText(newText);
                }
            });
        },
        setText: function(newText) {
            this.comp.attr({
                text : newText
            });
        },
        getText: function() {
            return this.comp.attr("text");
        },
        draw:function() {
            var pos = this.path.linePoints.midDot().pos();
            this.comp.attr({
                x : pos.x + this.path.pathConfig.textPos.x,
                y : pos.y + this.path.pathConfig.textPos.y
            });
        },
        remove: function() {
            this.comp.remove();
            try {
                $(this.paper).unbind("textchange", c);
            } catch (o) {
            }
        }
    });
})(jQuery);
